#ifndef __ILIB_TEST_TP_OTCP_H__
#define __ILIB_TEST_TP_OTCP_H__

#include "../util/h/iLogger.h"
#include "../util/h/iTimer.h"
#include "../tp/iTPConnection.h"
#include "../tp/iTPListener.h"
#include "app.h"

class OTCPTester : public iTPConnection::iSink
                 , public iTPListener::iSink
				 , public iTimer::iSink
{
public:
    OTCPTester(iTPConnection* conn)
    :m_conn(conn)
    {
    }
	void on_timer(iTimer* tmr)
	{
		switch(tmr->get_id())
		{
		case 2012:
            {
                iOutputStream os;
                std::string msg = "this is message from client";
                os << msg;
                m_conn->send_data(os.get_msg_db());
            }
            break;
        case 2013:
            {
                iOutputStream os;
                std::string msg = "this is message from server";
                os << msg;
                m_conn->send_data(os.get_msg_db()->duplicate());
            }
            break;
		}
	}
    void on_connect_confirm(int32_t result, iMsgDB* rsp)
    {
        char local[64]={0}, remote[64]={0};
        m_conn->get_option(TP_OPT_LOCAL_ADDR,  local,  sizeof(local));
        m_conn->get_option(TP_OPT_REMOTE_ADDR, remote, sizeof(remote));
		INFO("connect confirm result="<<result<<",local="<<local<<",remote="<<remote);

		iTimer::new_instance(this, 2012, 100, 0);
    }
    void on_data_indication(iMsgDB* mbs)
    {
        INFO("on data indication,len="<<mbs->get_total_data_length()<<",dump:"<<mbs->dump());
    }
    void on_close(int32_t reason)
    {
        INFO("on close reason="<<reason);
    }

    virtual void on_connection_indication(iTPConnection* iConn, iMsgDB* req)
    {
        m_conn = iConn;
        char local[64]={0}, remote[64]={0};
        m_conn->get_option(TP_OPT_LOCAL_ADDR,  local,  sizeof(local));
        m_conn->get_option(TP_OPT_REMOTE_ADDR, remote, sizeof(remote));
        INFO("on connection indication, local="<<local<<",remote="<<remote);
        iConn->connect_response(NULL, this);
        
		//iTimer::new_instance(this, 2013, 100, 0);
    }
	
private:
    iTPConnection*  m_conn;
};

#endif //__ILIB_TEST_TP_OTCP_H__
